#include "homeinterface.h"
#include "ui_homeinterface.h"
#include <QDebug>


struct Pack
{
    int dataType;
    int deviceId;
    char opt[16];
};


HomeInterface::HomeInterface(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::HomeInterface),
  tcpSocket(new QTcpSocket(this))
{
    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/image/homeIcon.ico"));
    this->setWindowTitle("智能家居系统");

    flagLed = false;
    flagFan = false;
    flagSpeaker = false;


    struct Pack pack;
    pack.dataType = 1;
    pack.deviceId = 16;
    connect(tcpSocket, &QTcpSocket::connected, this, [=](){
        ui->textBrowser->insertPlainText("connect ok\n");
        tcpSocket->write((char *)&pack, sizeof(pack));
    });

    connect(tcpSocket, &QTcpSocket::readyRead, this, [=](){
       QByteArray recvDate = tcpSocket->readAll();
       struct Pack pack;
       memset(&pack, 0, sizeof(pack));
       char *p = recvDate.data();
       for (int i = 0; i < recvDate.size(); i++)
       {
           ((char *)(&pack))[i] = p[i];
       }
       ui->textBrowser->insertPlainText(pack.opt);

       if (strcmp(pack.opt, "led on ok") == 0)
       {
           ui->pushButton_room->setStyleSheet("border-image:url(:/image/led_on.png");
           if (flagLed == false)
           {
               flagLed = true;
           }
       }
       else if (strcmp(pack.opt, "led off ok") == 0)
       {
            ui->pushButton_room->setStyleSheet("border-image:url(:/image/led_off.png");
            if (flagLed == true)
            {
                flagLed = false;
            }
       }
    });


    tcpSocket->connectToHost(ui->lineEdit->text(),ui->lineEdit_2->text().toInt());
}

HomeInterface::~HomeInterface()
{
    delete ui;
}


void HomeInterface::closeEvent(QCloseEvent *event)
{
    qDebug() <<"closeEvent(QCloseEvent *event)";
    tcpSocket->disconnectFromHost();
    QWidget::closeEvent(event);
}


void HomeInterface::on_pushButton_return_clicked()
{
    emit signalReturn();
}



void HomeInterface::on_pushButton_room_clicked()
{
    qDebug() << "灯光控制";
    flagLed = !flagLed;
    if(flagLed==true)
    {
        struct Pack pack;
        pack.dataType = 2;
        pack.deviceId = 26;
        strcpy(pack.opt, "led on");
        tcpSocket->write((char *)&pack, sizeof(pack));
        //ui->pushButton_room->setStyleSheet("border-image:url(:/image/led_on.png)");
        //tcpSocket->write("led on\n");
        ui->textBrowser->insertPlainText("房间灯光开启\n");
    }
    else
    {
        struct Pack pack;
        pack.dataType = 2;
        pack.deviceId = 26;
        strcpy(pack.opt, "led off");
        tcpSocket->write((char *)&pack, sizeof(pack));
        //ui->pushButton_room->setStyleSheet("border-image:url(:/image/led_off.png)");
        //tcpSocket->write("led off\n");
        ui->textBrowser->insertPlainText("房间灯光关闭\n");
    }
}


void HomeInterface::on_pushButton_wendu_clicked()
{

    qDebug() << "温度控制";
    flagFan = !flagFan;
    if(flagFan==true)
    {
        struct Pack pack;
        pack.dataType = 2;
        pack.deviceId = 26;
        strcpy(pack.opt, "wendu on");

        tcpSocket->write((char *)&pack, sizeof(pack));

        ui->pushButton_wendu->setStyleSheet("border-image:url(:/image/wendu_on.png)");
//        tcpSocket->write("wendu on\n");
        ui->textBrowser->insertPlainText("风扇开启\n");
    }
    else
    {
        struct Pack pack;
        pack.dataType = 2;
        pack.deviceId = 26;
        strcpy(pack.opt, "wendu off");

        tcpSocket->write((char *)&pack, sizeof(pack));

        ui->pushButton_wendu->setStyleSheet("border-image:url(:/image/wendu_off.png)");
//        tcpSocket->write("wendu off\n");
        ui->textBrowser->insertPlainText("风扇关闭\n");
    }
}


void HomeInterface::on_pushButton_yule_clicked()
{
    qDebug() << "娱乐设备";
      flagSpeaker = !flagSpeaker;
      if(flagSpeaker==true)
      {

          struct Pack pack;
          pack.dataType = 2;
          pack.deviceId = 26;
          strcpy(pack.opt, "speaker on");

          tcpSocket->write((char *)&pack, sizeof(pack));


          ui->pushButton_yule->setStyleSheet("border-image:url(:/image/yule_on.png)");
  //        tcpSocket->write("speaker on\n");
          ui->textBrowser->insertPlainText("娱乐设备开启\n");
      }
      else
      {
          struct Pack pack;
          pack.dataType = 2;
          pack.deviceId = 26;
          strcpy(pack.opt, "speaker off");

          tcpSocket->write((char *)&pack, sizeof(pack));
          ui->pushButton_yule->setStyleSheet("border-image:url(:/image/yule_off.png)");
  //        tcpSocket->write("speaker off\n");
          ui->textBrowser->insertPlainText("娱乐设备关闭\n");
      }
}

